/**
 * Implementation of DataProvider interface that loads data from XML file
 *
 * @author Krzysztof Goj
 */

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class GnuplotExporter {
    public String filename(String base) {
        return base.replace(' ', '_').concat(".gp");
    }
    class MyHandler extends DefaultHandler {
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if(qName.equals("data")) {
                try {
                    String valueDataTypeName = attributes.getValue("type");
                    valueDataType = Class.forName(valueDataTypeName);
                } catch(ClassNotFoundException e) {
                    throw new SAXException(e);
                }
            }
            else if(qName.equals("series")) {
                String fn = filename(attributes.getValue("name"));
                try {
                    seriesWriter = new PrintWriter(new FileWriter(fn));
                } catch(IOException e) {
                    throw new SAXException(e);
                }

                if(!first)
                    mainWriter.print(", ");
                mainWriter.print("\"");
                mainWriter.print(fn);
                mainWriter.print("\"");
                mainWriter.print(printWith);
                first = false;
            }
            else if(qName.equals("value")) {
                if(Object[].class.isAssignableFrom(valueDataType)) {
                    String xyz[] = {"x", "y", "z"};
                    for(int i=0; i<3; ++i) 
                        if(attributes.getValue(xyz[i]) != null) {
                            Number nr = new Double(attributes.getValue(xyz[i]));
                            seriesWriter.print(nr);
                            seriesWriter.print(" ");
                        }
                    seriesWriter.println();
                }
            }
	}
        public void endElement(String uri, String localName, String qName) {
            if(qName.equals("series")) {
                seriesWriter.println();
                seriesWriter.close();
            }
	}
    }
    public void parse(InputStream is) throws Exception {
	ContentHandler myHandler = new MyHandler();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        SAXParser parser = factory.newSAXParser();
        parser.parse(is, new MyHandler());
    }

    public GnuplotExporter(String filename) throws Exception {
        String fn = filename.replaceFirst(".*/", "").replaceFirst("\\.xml$", "").concat(".gp");
        mainWriter = new PrintWriter(new FileWriter(fn));
        mainWriter.println("set terminal jpeg");
        mainWriter.print("set output \"");
        mainWriter.print(fn.replaceFirst("\\.gp$", ".jpg"));
        mainWriter.println("\"");

        mainWriter.print("plot ");
        parse(new FileInputStream(filename));
        mainWriter.println();
        mainWriter.close();
    }

    private PrintWriter seriesWriter, mainWriter;
    private boolean first=true;
    private static String printWith="";
    private Class valueDataType;

    public static void main(String args[]) throws Exception {
        if(args.length >= 2)
            printWith = "with ".concat(args[1]);
        if(args.length > 0)
            new GnuplotExporter(args[0]);
        else
            System.err.println("No arguments.");
    }
}


